﻿Imports BUS

Public Class frmSach

    Dim bt As Button
    Sub New(ByRef bt As Button)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.bt = bt
    End Sub

    Private Sub frmSach_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim dt As DataTable = CuonSach_BUS.layTatCaThongTinTheoMa(bt.Tag)

        pbHinhSach.BackgroundImageLayout = ImageLayout.Stretch
        With dt
            pbHinhSach.BackgroundImage = Image.FromFile(.Rows(0)(0).ToString)
            lblTuaSach.Text = .Rows(0)("TuaSach").ToString
            lblTacGia.Text = .Rows(0)("TacGia").ToString
            lblSoTrang.Text = .Rows(0)("SoTrang").ToString
            lblNamSX.Text = .Rows(0)("NamSanXuat").ToString
            lblSLSach.Text = .Rows(0)("SoLuongSach").ToString
            lblTomTat.Text = .Rows(0)("TomTat").ToString
            lblLoaiSach.Text = .Rows(0)("TenLoaiSach").ToString
            lblGiaTien.Text = Single.Parse(.Rows(0)("GiaTien").ToString).ToString
            lblNhaXuatBan.Text = .Rows(0)("TenNXB").ToString
            lblKe.Text = .Rows(0)("TenKe").ToString
            lblNgan.Text = .Rows(0)("ViTriNgan").ToString
            lblTang.Text = .Rows(0)("ViTriTang").ToString
            lblTinhTrang.Text = .Rows(0)("TenTinhTrang").ToString
        End With

        If frmDangNhap.dsSachTrongGio.Count = 0 Then
            Return
        End If

        For i As Integer = 0 To frmDangNhap.dsSachTrongGio.Count - 1
            If frmDangNhap.dsSachTrongGio(i).maSach = bt.Tag Then
                lblSLSach.Text = Integer.Parse(lblSLSach.Text) - _
                    frmDangNhap.dsSachTrongGio(i).soLuong
                If Integer.Parse(lblSLSach.Text) = 0 Then
                    lblTinhTrang.Text = "Đã mượn hết"
                End If
            End If
        Next



    End Sub

    Private Sub btnMuonSach_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMuonSach.Click
        If lblTinhTrang.Text = "Đã mượn hết" Or Integer.Parse(lblSLSach.Text) = 0 Then
            MessageBox.Show("Sách bạn vừa chọn đã được mượn hết. Vui lòng mượn vào lúc khác")
            Return
        End If

        Dim kt As Boolean = False

        For i As Integer = 0 To frmDangNhap.dsSachTrongGio.Count - 1
            If frmDangNhap.dsSachTrongGio(i).maSach = bt.Tag Then
                frmDangNhap.dsSachTrongGio(i).soLuong = _
                    frmDangNhap.dsSachTrongGio(i).soLuong + 1
                frmDangNhap.dsSachTrongGio(i).tienTheChan = _
                    frmDangNhap.dsSachTrongGio(i).tienTheChan * _
                    frmDangNhap.dsSachTrongGio(i).soLuong
                kt = True
                MessageBox.Show("Đã đưa vào giỏ!")
                frmSach_Load(sender, e)
                Return
            End If
        Next

        If kt = False Then
            Dim gs As New GioSach
            gs.maSach = bt.Tag
            gs.tenSach = lblTuaSach.Text
            gs.loaiSach = lblLoaiSach.Text
            gs.soLuong = 1
            gs.tienTheChan = Single.Parse(lblGiaTien.Text) * 0.2

            frmDangNhap.dsSachTrongGio.Add(gs)
            MessageBox.Show("Đã đưa vào giỏ!")
            frmSach_Load(sender, e)
        End If
    End Sub
End Class